﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using jKinect;
using System.Web;
using System.Reflection;
using jKinect.Connector;

namespace jKinect.Browser
{
    /// <summary>
    /// Interaction logic for jKinectBrowser.xaml
    /// </summary>
    public partial class jKinectBrowserControl : UserControl
    {
        public jKinectInjector jKinectInjector { get; set; }

        public WebBrowser WebBrowser
        {
            get { return webBrowser; }
            set { webBrowser = value; }
        }

        // Summary:
        //     Occurs when the document being navigated to has finished downloading.
        public event LoadCompletedEventHandler LoadCompleted;
        //
        // Summary:
        //     Occurs when the document being navigated to is located and has started downloading.
        public event NavigatedEventHandler Navigated;
        //
        // Summary:
        //     Occurs just before navigation to a document.
        public event NavigatingCancelEventHandler Navigating;

        public event EventHandler<EventArgs> OnQuit;

        public jKinectBrowserControl()
        {
            InitializeComponent();
            jKinectInjector = new jKinectInjector(webBrowser);
            jKinectInjector.OnQuit += new EventHandler<EventArgs>(jKinectInjector_OnQuit);
            webBrowser.Navigating += new NavigatingCancelEventHandler(webBrowser_Navigating);
            webBrowser.Navigated += new NavigatedEventHandler(webBrowser_Navigated);
            webBrowser.LoadCompleted += new LoadCompletedEventHandler(webBrowser_LoadCompleted);
            Dispatcher.ShutdownStarted += new EventHandler(Dispatcher_ShutdownStarted);
            webBrowser.KeyUp += new KeyEventHandler(webBrowser_KeyUp);
        }

        void jKinectInjector_OnQuit(object sender, EventArgs e)
        {
            if (OnQuit != null)
                OnQuit(sender, e);
        }

        void webBrowser_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.F5:
                    // force full navigation(navigated event) on refresh instead of just simple reload
                    // see note on http://msdn.microsoft.com/en-us/library/txewax99.aspx
                    e.Handled = true;
                    webBrowser.Navigate(webBrowser.Source);
                    break;
            }
        }

        void Dispatcher_ShutdownStarted(object sender, EventArgs e)
        {
            jKinectInjector.Dispose();
        }

        void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            jKinectInjector.Dispose();
            if (Navigating != null)
                Navigating(sender, e);
        }

        void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            if (Navigated != null)
                Navigated(sender, e);
        }

        void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            jKinectInjector.Inject_jKinectScript();
            if (LoadCompleted != null)
                LoadCompleted(sender, e);
        }

        public void Navigate(Uri uri)
        {
            webBrowser.Navigate(uri, null, null, "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; jKinect/" + Assembly.GetEntryAssembly().GetName().Version);
        }
    }
}
